define(['require' , 'exports' , 'module' , 'jquery' , 'layer'  ] , function (require , exports , module , $) {

    var ajaxPost = function (url , data , callback) {
        $.ajax({
            url     : url,
            type    : 'POST',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data : data ,
            dataType : 'json' ,
            success : function (data) {
                callback(null , data);
            },
            error : function (error) {
                callback(error , {});
            }
        });
    };


    var GetRequest = function (obj) {
        var protocol = location.protocol;
        var host = location.host;
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    };


    var buttonA = function () {
        $('.button-a').bind('click' , function () {
            window.location.href = $(this).attr('href');
        });

    };
    
    var getCartNum = function () {
        $.ajax({
            url : '/order/cart/num',
            type : 'GET',
            dataType : 'JSON',
            success : function (data) {
                if(data.data > 0){
                    $('.track-point').show();
                }
            }
        });  
    };


    var addCart = function () {
        $('#content').on('click' , '.addCart' ,function (e) {
            e.stopPropagation();
            var gid         = $(this).attr('gid');
            var buyNumItem  = $('.buy-num-'+gid);
            var buyNum      = 0;
            var price       = $('.now-price-'+gid);

            if(buyNumItem.is('input')){
                buyNum = buyNumItem.val() * 1 + 1;
            }else{
                buyNum = buyNumItem.text() * 1 + 1;
            }

            ajaxPost('/order/update-cart' , {gid : gid , buy_num : buyNum} , function (error , data) {
                if(error === null && data.code == 0) {

                    if(buyNumItem.is('input')){
                        buyNumItem.val(buyNum);
                    }else{
                        buyNumItem.text(buyNum);
                    }

                    buyNumItem.css({
                        visibility:'visible'
                    });
                    $('.reduce-' + gid).css({
                        visibility:'visible'
                    });

                    countPrice();
                }else{
                    layer.open({
                        content : '添加购物车失败' ,
                        skin : 'msg' ,
                        time : 2
                    });
                }
            });


        })
    };


    var reduceCart = function () {
        $('#content').on('click' , '.reduceCart' ,function () {
            var gid         = $(this).attr('gid');
            var buyNumItem  = $('.buy-num-'+gid);
            var finded      = false;
            var buyNum      = 0;

            if(buyNumItem.is('input')){
                buyNum = buyNumItem.val() * 1 - 1;
            }else{
                buyNum = buyNumItem.text() * 1 - 1;
            }

            if(buyNum >= 0){

                ajaxPost('/order/update-cart' , {gid : gid , buy_num : buyNum} , function (error , data) {
                    if(error === null && data.code == 0) {

                        if(buyNumItem.is('input')){
                            if(buyNum == 0){
                                $('#cart-item-' + gid).remove();
                            }
                            buyNumItem.val(buyNum);
                        }else{
                            buyNumItem.text(buyNum);
                            if(buyNum == 0){
                                buyNumItem.css({
                                    visibility:'hidden'
                                });
                                $('.reduce-' + gid).css({
                                    visibility:'hidden'
                                });
                            }
                        }

                        countPrice();
                    }else{
                        layer.open({
                            content : '添加购物车失败' ,
                            skin : 'msg' ,
                            time : 2
                        });
                    }
                });

            }else{
                layer.open({
                    content : '已经没有了' ,
                    skin : 'msg' ,
                    time : 2
                });
            }
        })
    };


    var countPrice =  function () {
        var totalPrice = 0;
        var buyNum = $('.buy-num');
        $('.price').each(function (index , p) {
            var price = $(p).text();
            var num = $(buyNum[index]).val();

            totalPrice += price * num;
        });

        if(totalPrice < 30){
            var differencePriceItem = $('#difference_price');

            differencePriceItem.text((30 - totalPrice).toFixed(2));

            $('.starting-price').show();
        }else{
            $('.starting-price').hide();
        }

        $('.total-price-num').text(totalPrice.toFixed(2));
    };

    var setLngLat = function (lng , lat) {
        window.sessionStorage.setItem('lng' , lng);
        window.sessionStorage.setItem('lat' , lat);
    };

    var getLngLat = function () {
        var lng = window.sessionStorage.getItem('lng');
        var lat = window.sessionStorage.getItem('lat');
        return {lng : lng , lat : lat};
    };

    var removeLngLat = function () {
        window.sessionStorage.removeItem('lng');
        window.sessionStorage.removeItem('lat');
    };


    var switchView = function () {
        $('.switch-buttom').bind('click' , function () {
            var __this = $(this);
            var view = __this.attr('view');

            $('.switch-buttom').removeClass('active');
            __this.addClass('active');
            $('.switch-view').hide();
            $('.switch-view-' + view).show();
        });
    };

    var confirm = function () {
        
        $('.confirm').bind('click' , function (e) {
            e.stopPropagation();

            var __this = $(this);

            var content = __this.attr('content');
            var type    = __this.attr('type');

            layer.open({
                content: content
                ,btn: ['是的', '点错了']
                ,yes: function(index){
                    if(type == 'skip'){
                        var url = __this.attr('url');
                        $.ajax({
                            'url'       : url,
                            'type'      : 'GET',
                            'dataType'  : 'JSON',
                            'success'   : function (data) {
                                if(data.code == 0){
                                    window.location.reload();
                                }else{
                                    layer.open({
                                        content : data.msg ,
                                        skin : 'msg' ,
                                        time : 2
                                    });
                                }
                            },
                            'error'     : function (error) {
                                layer.open({
                                    content : '服务器链接错误' ,
                                    skin : 'msg' ,
                                    time : 2
                                });
                            }
                        });
                    }
                    layer.close(index);
                }
            });
        });

    };
    
    exports.confirm = confirm;

    exports.switchView = switchView;
    exports.getCartNum = getCartNum;

    exports.setLngLat = setLngLat;
    exports.getLngLat = getLngLat;
    exports.removeLngLat = removeLngLat;

    exports.buttonA = buttonA;
    exports.ajaxPost = ajaxPost;
    exports.addCart = addCart;
    exports.reduceCart = reduceCart;
    exports.countPrice = countPrice;

});
